package com.ktjiaoyu.crm.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @ClassName : AuthorizationFilter
 * @Description :
 * @Author : Lzz
 * @Date: 2023/11/17 10:36:11
 */
public class AuthorizationFilter implements Filter {


    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        if(httpRequest.getSession().getAttribute("loginUser")==null){
            httpResponse.setContentType("text/html;charset=UTF-8");
            PrintWriter out = httpResponse.getWriter();
            out.print("<script>alert('请先进行登陆， 再进行后续操作！ (Filter 控制)');location.href='"+httpRequest.getContextPath()+"/toLogin';</script>");
        }else{
            chain.doFilter(httpRequest,httpResponse);
        }
    }
}
